// controller/user.js
'use strict';
const Controller = require('egg').Controller;
class UserController extends Controller {
  async index() {
    const data = await this.ctx.service.user.findAll(this.ctx);
    this.ctx.body = data;
  }

  async show() {
    const ctx = this.ctx;
    const id = ctx.params.id;
    const user = await this.ctx.service.user.find(id);
    ctx.body = user;
  }

  async update() {
    const ctx = this.ctx;
    const userId = ctx.params.id;
    const params = ctx.request.body;
    const change = await ctx.service.user.update(userId, params);
    ctx.body = change;
  }

  async login() {
    const { ctx, app } = this;
    const { username, password } = ctx.request.body;
    const userInfo = await ctx.service.user.getUserByName(username);
    if (!userInfo || !userInfo.id) {
      ctx.body = {
        code: 500,
        msg: '账号不存在',
        success: false,
        data: null,
      };
      return;
    }
    if (userInfo && password !== userInfo.password) {
      ctx.body = {
        code: 500,
        msg: '账号密码错误',
        success: false,
        data: null,
      };
      return;
    }
    // token 生成
    const token = app.jwt.sign(
      {
        id: userInfo.id,
        username: userInfo.username,
        exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60, // token 有效期为 24 小时
      },
      app.config.jwt.secret
    );
    ctx.body = {
      code: 200,
      message: '登录成功',
      success: true,
      data: {
        token,
      },
    };
  }

  async logins() {
    const { ctx, app } = this;
    const { username } = ctx.request.body;
    const userInfo = await ctx.service.user.getUserByName(username);
    if (!userInfo || !userInfo.id) {
      ctx.body = {
        code: 500,
        msg: '账号不存在',
        success: false,
        data: null,
      };
      return;
    }
    // token 生成
    const token = app.jwt.sign(
      {
        id: userInfo.id,
        username: userInfo.username,
        exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60, // token 有效期为 24 小时
      },
      app.config.jwt.secret
    );
    ctx.body = {
      code: 200,
      message: '登录成功',
      success: true,
      data: {
        token,
      },
    };
  }

  async register() {
    const { ctx } = this;
    const { username, password, nickname } = ctx.request.body;
    const userInfo = await ctx.service.user.getUserByName(username);
    // 判断是否已经存在
    if (userInfo && userInfo.id) {
      ctx.body = {
        code: 500,
        msg: '账户名已被注册，请重新输入',
        data: null,
        success: false,
      };
      return;
    }
    const defaultAvatar = 'http://127.0.0.1:7001/public/user/132.png';
    const result = await ctx.service.user.register({
      username,
      password,
      nickname,
      avatar: defaultAvatar,
    });
    if (result) {
      ctx.body = {
        code: 200,
        msg: '注册成功',
        data: null,
        success: true,
      };
    } else {
      ctx.body = {
        code: 500,
        msg: '注册失败',
        data: null,
        success: false,
      };
    }
  }
  // 获取个人信息
  async getUserInfo() {
    const { ctx, app } = this;
    const token = ctx.request.header.authorization;
    const decode = await app.jwt.verify(token, app.config.jwt.secret);
    // 通过 token 带过来的 username 获取到个人信息
    const userInfo = await ctx.service.user.getUserByName(decode.username);
    const defaultAvatar = 'https://profile.csdnimg.cn/4/2/E/3_weixin_38992765';
    ctx.body = {
      code: 200,
      msg: '请求成功',
      data: {
        id: userInfo.id,
        username: userInfo.username,
        iphone: userInfo.iphone,
        avatar: userInfo.avatar || defaultAvatar,
        nickname: userInfo.nickname || '不知名先生',
      },
    };
  }
}
module.exports = UserController;
